#!/bin/bash

echo "🎵 音乐格式转换器启动脚本"
echo "================================"

# 检查Java版本
echo "检查Java版本..."
java_version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2 | cut -d'.' -f1)
if [ "$java_version" -lt 17 ]; then
    echo "❌ 错误: 需要Java 17或更高版本，当前版本: $java_version"
    exit 1
fi
echo "✅ Java版本检查通过: $(java -version 2>&1 | head -n 1)"

# 检查Maven
echo "检查Maven..."
if ! command -v mvn &> /dev/null; then
    echo "❌ 错误: 未找到Maven，请先安装Maven"
    exit 1
fi
echo "✅ Maven检查通过: $(mvn -version | head -n 1)"

# 检查FFmpeg
echo "检查FFmpeg..."
if ! command -v ffmpeg &> /dev/null; then
    echo "⚠️  警告: 未找到FFmpeg，请先安装FFmpeg"
    echo "安装方法:"
    echo "  macOS: brew install ffmpeg"
    echo "  Ubuntu: sudo apt install ffmpeg"
    echo "  Windows: 下载并安装FFmpeg"
    echo ""
    echo "是否继续启动应用？(y/n)"
    read -r response
    if [[ ! "$response" =~ ^[Yy]$ ]]; then
        exit 1
    fi
else
    echo "✅ FFmpeg检查通过: $(ffmpeg -version | head -n 1)"
fi

# 创建必要的目录
echo "创建必要的目录..."
mkdir -p uploads outputs

# 编译项目
echo "编译项目..."
mvn clean compile

# 启动应用
echo "启动应用..."
echo "应用将在 http://localhost:8080 启动"
echo "按 Ctrl+C 停止应用"
echo ""

mvn spring-boot:run 